/**  
* <p>Title: ParkingService.java</p >  
* <p>Description: </p >  
* <p>Company:luo.com</p >  
* @author 魏家洛 
* @date 2019年1月24日  
* @version 1.0  
*/  
package com.pmsystem.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.FreeCarMapper;
import com.pmsystem.dao.ParkingMapper;
import com.pmsystem.dao.PayServerMapper;
import com.pmsystem.dao.ZoneParkingMapper;
import com.pmsystem.pojo.Parking;
import com.pmsystem.pojo.PayServer;
import com.pmsystem.pojo.ZoneParking;
import com.pmsystem.util.account.CashAccount;
import com.pmsystem.util.account.CashAccountImpl;

/**  
* <p>Title: ParkingService</p >  
* <p>Description: </p >  
* @author 魏家洛  
* @date 2019年1月24日  
*/
@Service
public class ParkingService {
	@Autowired
	ParkingMapper parkingMapper;
	@Autowired
	FreeCarMapper freeCarMapper;
	@Autowired
	ZoneParkingMapper zoneParkingMapper;
	@Autowired
	PayServerMapper payServerMapper;

	/**  
	 * <p>Title: carIn</p >  
	 * <p>Description:登记车辆进入停车场</p >  
	 * @author 魏家洛
	 * @param parking
	 * @return  
	 */  
	public boolean carIn(Parking parking) {
		if(parkingMapper.checkCarLicenseIsInParking(parking.getCarlicense())==0)
			return (parkingMapper.insertParking(parking)==1);
		else
			return false;
	}

	/**  
	 * <p>Title: checkParking</p >  
	 * <p>Description:检查车牌号对应车是否在停车场内停车</p >  
	 * @author 魏家洛
	 * @param carlicense
	 * @return  
	 */  
	public boolean checkParking(String carlicense) {
		return (parkingMapper.checkCarLicenseIsInParking(carlicense)==1);
	}
	/**  
	 * <p>Title: billing</p >  
	 * <p>Description: </p >  
	 * @author 魏家洛
	 * @param carlicense
	 * @return  
	 */  
	public Float billing(String carlicense,Date parkingEndTime) {
		PayServer payServer = payServerMapper.selectPayServerByName("guestparking");
		Parking parking = parkingMapper.selectParkingByCarLicenseWithNotPay(carlicense);
		//float time = ((parkingEndTime.getTime()-parking.getParkingStartTime().getTime())/(1000*60.0f));
		float time = 120f;
		CashAccount cashAccount = new CashAccountImpl();
		Map<String, Float> map = new HashMap<>();
		Float cash =null;
		map.put("param1", payServer.getParam1());
		map.put("param2", payServer.getParam2());
		map.put("param3", payServer.getParam3());
		map.put("param4", payServer.getParam4());
		map.put("param5", payServer.getParam5());
		System.out.println(payServer);
		cash =	cashAccount.ParkingCashAccount(payServer.getPayRule(), time, map);
		System.out.println(cash);
		if(cash==null) {
			return -1f;
		}else {
			if(freeCarMapper.isExistLicense(carlicense)!=0)
				cash=0f;
			parking.setParkingEndTime(parkingEndTime);
			parking.setCash(cash);
			if(parkingMapper.updateParkingEndTimeAndCash(parking)==1)
				return cash;
			else
				return -1f;
		}
	}

	/**  
	 * <p>Title: remainSpace</p >  
	 * <p>Description:查找对应停车分区剩余车位</p >  
	 * @author 魏家洛
	 * @param parkingArea
	 * @return  
	 */  
	public int remainSpace(Integer parkingArea) {
		int useSpace = parkingMapper.selectUseParkingAreaCount(parkingArea);
		ZoneParking zoneParking = zoneParkingMapper.selectById(parkingArea);
		return (zoneParking.getTotal()-useSpace);
	}

	/**  
	 * <p>Title: catOut</p >  
	 * <p>Description:车牌号对应车辆驶出停车场</p >  
	 * @author 魏家洛
	 * @param carlicense
	 * @return  
	 */  
	public boolean carOut(String carlicense) {
		return (parkingMapper.updateParkingBePay(carlicense)!=0);
	}
}
